package com.example.administrator.mymusic.zhong;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.example.administrator.mymusic.Class.LrcClass;
import com.example.administrator.mymusic.Class.LrcListView;
import com.example.administrator.mymusic.Class.LrcProcess;
import com.example.administrator.mymusic.Class.MyLrcAdapter;
import com.example.administrator.mymusic.Class.SongsClass;
import com.example.administrator.mymusic.MainActivity;
import com.example.administrator.mymusic.R;

import java.util.List;

/**
 * Created by Administrator on 2013/1/2.
 */
public class LrcFragment extends Fragment {
    private LrcProcess lrcProcess;
    private List<LrcClass> lrcClassList;
    private List<SongsClass> songsClassList;
    private MainActivity mainActivity;
    private LrcListView listView;
    private MyLrcAdapter myLrcAdapter;
    private boolean start = true;
    private int index = 0;
    private int showindex = 0;
    private int seelpTime = 1000;
    private boolean noLrc = true;
    private int frist = 0;

    public LrcListView getListView() {
        return listView;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhong_lrc, null);
        mainActivity = (MainActivity) getActivity();
        songsClassList = mainActivity.getDatas();
        listView = (LrcListView) view.findViewById(R.id.lv_fr_zhong_lrc);
        listView.setAdapter(lrc(0));
        listView.setEmptyView(view.findViewById(R.id.null_lrc));
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                myLrcAdapter.setVisibleItem(firstVisibleItem, visibleItemCount, totalItemCount);
                myLrcAdapter.notifyDataSetChanged();
            }
        });
        Log.d("55555", "onCreateView: " + (listView == null));
        new Thread() {
            @Override
            public void run() {
                super.run();
                while (start) {
                    while (!noLrc) {
                        index = getIndex(mainActivity.getPlaySeek());
                        Log.d("888887777", "run: " + index);
                        if (showindex < index) {
                            showindex = index;
                            Log.d("888887777", "runhand: " + index);
//                            Message message = new Message();
//                            Bundle bundle = new Bundle();
//                            bundle.putInt("index", showindex);
//                            message.setData(bundle);
//                            handler.sendMessage(message);
                            handler.sendMessageAtTime(new Message(), 0);
                        }

//                        Log.d("qazwsx", "run: " + index);
//                        if (index >= 1) {
//                            seelpTime = lrcClassList.get(index).getLrctime() - lrcClassList.get(index - 1).getLrctime();
//                        } else {
//                            seelpTime = lrcClassList.get(0).getLrctime();
//                        }
//                        try {
//                            Thread.sleep(seelpTime);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                    }
                }
            }
        }.start();
        return view;
    }

    //    private android.os.Handler handler = new android.os.Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
////            int i = msg.getData().getInt("index");
////            Log.d("666666", "handleMessage: " + i);
////            listView.smoothScrollBy(120, lrcClassList.get(index).getLrctime() - lrcClassList.get(index - 1).getLrctime());
//            listView.smoothScrollByOffset(i);
//            listView.smoothScrollToPosition(1);
//            listView.setSelection(i);
//        }
//    };
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
//            int i = msg.getData().getInt("index");
            listView.smoothScrollBy(120, lrcClassList.get(index).getLrctime() - lrcClassList.get(index - 1).getLrctime());
//            listView.smoothScrollByOffset(i);
//            listView.smoothScrollToPosition(1);
//            listView.setSelection(i);
            return false;
        }
    });

    public int getIndex(int PlaySeek) {
        for (int i = 0; i < lrcClassList.size() - 1; i++) {
            if (lrcClassList.get(i).getLrctime() <= PlaySeek && PlaySeek <= lrcClassList.get(i + 1).getLrctime()) {
                return i + 1;
            }
        }
        return 0;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        start = false;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("55555", "onCreate: ");
    }

    public class LrcBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            int i = intent.getIntExtra("songNum", 0);
            listView.setAdapter(lrc(i));
            listView.setSelection(0);
        }
    }

    public MyLrcAdapter lrc(int i) {
        lrcProcess = new LrcProcess();

        lrcClassList = lrcProcess.readLrc(songsClassList.get(i).getName());
        try {
            lrcClassList.get(8);
            noLrc = false;
            showindex = 0;
        } catch (Exception e) {
            Log.d("88888", "没有歌词! ");
            noLrc = true;
        }
        Log.d("88888", "lrc: " + noLrc);
//        Log.d("55555", "onCreateView: " + (lrcClassList == null));
        myLrcAdapter = new MyLrcAdapter(getActivity(), lrcClassList);
//        listView.onTouchEvent();

        return myLrcAdapter;
    }
}
